<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="68px">
			<el-form-item label="仓库编号" prop="warehouseCode">
				<el-input v-model="queryParams.warehouseCode" placeholder="请输入仓库编号" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="仓库名称" prop="warehouseName">
				<el-input v-model="queryParams.warehouseName" placeholder="请输入仓库名称" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="仓库状态" prop="warehouseStatus">
				<el-select v-model="queryParams.warehouseStatus" placeholder="请选择仓库状态" clearable>
					<el-option v-for="dict in dict.type.py_warehouse_status" :key="dict.value" :label="dict.label"
						:value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
					v-hasPermi="['warehouse:warehouse:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
					v-hasPermi="['warehouse:warehouse:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
					@click="handleDelete" v-hasPermi="['warehouse:warehouse:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
					v-hasPermi="['warehouse:warehouse:export']">导出</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="warehouseList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="排序" align="center" prop="" sortable >
        <template slot-scope="scope">
          {{scope.$index+1}}
        </template>
      </el-table-column>
			<el-table-column label="仓库编号" align="center" prop="warehouseCode">
				<template #default="scope">
					<a style="text-decoration: none;color: #41a863;"
						@click="handleDetails(scope.row)">{{ scope.row.warehouseCode }}</a>
				</template>
			</el-table-column>
			<el-table-column label="仓库名称" align="center" prop="warehouseName" />
			<el-table-column label="仓库容量" align="center" prop="warehouseVessel" />
			<el-table-column label="库管部门" align="center" prop="dept" />
			<el-table-column label="仓库主管" align="center" prop="manager" />
			<el-table-column label="仓库地址" align="center" prop="warehouseAddress" :show-overflow-tooltip="true" />
			<el-table-column label="库位数量" align="center">
				<template #default="scope">
					{{ locationCounts[scope.row.warehouseCode] }}
				</template>
			</el-table-column>
			<el-table-column label="仓库状态" align="center" prop="warehouseStatus">
				<template slot-scope="scope">
					<dict-tag v-if="scope.row.warehouseLock == 1" :options="dict.type.py_warehouse_lock" :value="scope.row.warehouseLock" />
					<dict-tag v-else :options="dict.type.py_warehouse_status" :value="scope.row.warehouseStatus" />
				</template>
			</el-table-column>
			<el-table-column label="操作人" align="center" prop="crateBy">
				<template slot-scope="scope">
					<span v-if="scope.row.updateBy">{{ scope.row.updateBy }}</span>
					<span v-else>{{ scope.row.crateBy }}</span>
				</template>
			</el-table-column>
			<el-table-column label="操作时间" align="center" prop="crateTime">
				<template slot-scope="scope">
					<span v-if="scope.row.updateTime">{{ parseTime(scope.row.updateTime) }}</span>
					<span v-else>{{ parseTime(scope.row.crateTime) }}</span>
				</template>
			</el-table-column>
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
						v-hasPermi="['warehouse:warehouse:edit']">修改</el-button>
					<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
						v-hasPermi="['warehouse:warehouse:remove']">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<!-- 添加或修改仓库列表对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="100px">
				<el-form-item label="仓库编号" prop="warehouseCode">
					<el-input v-model="form.warehouseCode" placeholder="请输入仓库编号" />
				</el-form-item>
				<el-form-item label="仓库名称" prop="warehouseName">
					<el-input v-model="form.warehouseName" placeholder="请输入仓库名称" />
				</el-form-item>
				<el-form-item label="仓库容量" prop="warehouseVessel">
					<el-input v-model="form.warehouseVessel" placeholder="请输入仓库容量" />
				</el-form-item>
				<el-form-item label="仓库地址" prop="warehouseAddress">
					<el-input v-model="form.warehouseAddress" placeholder="请输入仓库地址" />
				</el-form-item>
				<el-form-item label="排序" prop="warehouseSort">
					<el-input v-model="form.warehouseSort" placeholder="请输入排序" />
				</el-form-item>
				<el-form-item label="仓库状态" prop="warehouseStatus">
					<el-select v-model="form.warehouseStatus" placeholder="请选择仓库状态">
						<el-option v-for="dict in dict.type.py_warehouse_status" :key="dict.value" :label="dict.label"
							:value="dict.value"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="仓库备注" prop="warehouseRemarks">
					<el-input v-model="form.warehouseRemarks" type="textarea" placeholder="请输入内容" />
				</el-form-item>
				<el-form-item label="创建人" prop="crateBy">
					<el-input v-model="form.crateBy" placeholder="请输入创建人" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listWarehouse,
		getWarehouse,
		delWarehouse,
		addWarehouse,
		updateWarehouse,
		findBsWhLocationByCount,
	} from "@/api/warehouse/warehouse";

	export default {
		name: "Warehouse",
		dicts: ['py_warehouse_status','py_warehouse_lock'],
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 仓库列表表格数据
				warehouseList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 库位数量
				locationCounts: {},
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					warehouseCode: null,
					warehouseName: null,
					dept: null,
					manager: null,
					warehouseStatus: null,
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {
					warehouseId: [{
						required: true,
						message: "仓库主键ID不能为空",
						trigger: "blur"
					}],
					warehouseCode: [{
						required: true,
						message: "仓库编号不能为空",
						trigger: "blur"
					}],
					warehouseName: [{
						required: true,
						message: "仓库名称不能为空",
						trigger: "blur"
					}],
					isDefault: [{
						required: true,
						message: "默认仓库不能为空",
						trigger: "change"
					}],
				}
			};
		},
		created() {
			this.getList();

		},
		methods: {
			/** 查询仓库列表列表 */
			getList() {
				this.loading = true;
				listWarehouse(this.queryParams).then(response => {
					this.warehouseList = response.rows;
					this.total = response.total;
					this.loading = false;
					this.getLocationCount();
				});
			},
			/** 获取库位数量 */
			getLocationCount() {
				this.warehouseList.forEach(async (item) => {
					const count = await findBsWhLocationByCount(item.warehouseCode);
					this.$set(this.locationCounts, item.warehouseCode, count);
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					warehouseId: null,
					warehouseCode: null,
					warehouseName: null,
					warehouseVessel: null,
					dept: null,
					manager: null,
					warehouseAddress: null,
					warehouseSort: null,
					warehouseStatus: null,
					warehouseRemarks: null,
					isDelete: null,
					crateBy: null,
					crateTime: null,
					updateBy: null,
					updateTime: null,
					isDefault: null,
					warehouseLock: null,
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.warehouseId)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				// this.reset();
				// this.open = true;
				// this.title = "添加仓库列表";

				this.$router.push({
					path: '/wms/warehouse/warehouse/add',
					query: {}
				}); // 使用绝对路径
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				const warehouseId = row.warehouseId || this.ids
				getWarehouse(warehouseId).then(response => {
					this.form = response.data;
					this.open = true;
					this.title = "修改仓库列表";
				});
			},
			/* 修改按钮 */
			handleUpdate(row) {
				this.$router.push({
					path: '/wms/warehouse/warehouse/add',
					query: {
						warehouseId: row.warehouseId
					}
				}); // 使用绝对路径
			},
			/* 详细按钮 */
			handleDetails(row) {
				this.$router.push({
					path: '/wms/warehouse/warehouse/details',
					query: {
						warehouseId: row.warehouseId
					}
				}); // 使用绝对路径
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.warehouseId != null) {
							updateWarehouse(this.form).then(response => {
								this.$modal.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						} else {
							addWarehouse(this.form).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const warehouseIds = row.warehouseId || this.ids;
				this.$modal.confirm('是否确认删除仓库列表编号为"' + warehouseIds + '"的数据项？').then(function() {
					return delWarehouse(warehouseIds);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('warehouse/warehouse/export', {
					...this.queryParams
				}, `warehouse_${new Date().getTime()}.xlsx`)
			}
		}
	};
</script>
